# -*- coding: utf-8 -*-
import cv2
import numpy as np
def nothing(x):
    pass


img_raw = cv2.imread('IMG1.jpeg')

img_raw = cv2.resize(img_raw,(600,800))
gray = cv2.cvtColor(img_raw, cv2.COLOR_BGR2GRAY)
blur = cv2.GaussianBlur(gray, (5,5), 0)
canny = cv2.Canny(blur, 10, 100)
_, contours, hierarchy = cv2.findContours(canny, cv2.RETR_TREE,
                         cv2.CHAIN_APPROX_SIMPLE)

marks = np.zeros(img_raw.shape[:2], np.int32)
# findContours检测到的轮廓
imageContours = np.zeros(img_raw.shape[:2], np.uint8)

# 轮廓颜色
compCount = 0
index = 0
# 绘制每一个轮廓
for index in range(len(contours)):
    # print(len(contours[index]))
    if (len(contours[index]) > 100):


        # 对marks进行标记，对不同区域的轮廓使用不同的亮度绘制，相当于设置注水点，有多少个轮廓，就有多少个轮廓
        # 图像上不同线条的灰度值是不同的，底部略暗，越往上灰度越高
        marks = cv2.drawContours(marks, contours, index, (index, index, index), 1, 8, hierarchy)
        # 绘制轮廓，亮度一样
        imageContours = cv2.drawContours(imageContours, contours, index, (255, 255, 255), 1, 8, hierarchy)

# 查看 使用线性变换转换输入数组元素成8位无符号整型。
markerShows = cv2.convertScaleAbs(marks)
cv2.imshow('markerShows', imageContours)

# 使用分水岭算法
marks = cv2.watershed(img_raw, marks)
afterWatershed = cv2.convertScaleAbs(marks)
cv2.imshow('afterWatershed', afterWatershed)

# 生成随机颜色
colorTab = np.zeros((np.max(marks) + 1, 3))
# 生成0~255之间的随机数
for i in range(len(colorTab)):
    aa = np.random.uniform(0, 255)
    bb = np.random.uniform(0, 255)
    cc = np.random.uniform(0, 255)
    colorTab[i] = np.array([aa, bb, cc], np.uint8)

bgrImage = np.zeros(img_raw.shape, np.uint8)

# 遍历marks每一个元素值，对每一个区域进行颜色填充
for i in range(marks.shape[0]):
    for j in range(marks.shape[1]):
        # index值一样的像素表示在一个区域
        index = marks[i][j]
        # 判断是不是区域与区域之间的分界,如果是边界(-1)，则使用白色显示
        if index == -1:
            bgrImage[i][j] = np.array([255, 255, 255])
        else:
            bgrImage[i][j] = colorTab[index]
cv2.imshow('After ColorFill', bgrImage)

# 填充后与原始图像融合
result = cv2.addWeighted(img_raw, 0.6, bgrImage, 0.4, 0)
cv2.imshow('addWeighted', result)

cv2.waitKey(0)
cv2.destroyAllWindows()




